﻿using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Contracts.Report;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Kingdee.BOS.Core.Enums.BOSEnums;

namespace RK.ReportPlugin.Ronma
{
    [Description("Ronma_简单账表：采购库存表发送OA"), HotUpdate]
    public class PurInvReport: SysReportBaseService
    {
        public override void Initialize()
        {
            base.Initialize();
            this.ReportProperty.ReportType = ReportType.REPORTTYPE_NORMAL;
            this.ReportProperty.ReportName = new LocaleValue("采购库存报表", base.Context.UserLocale.LCID);
            this.IsCreateTempTableByPlugin = true;
            this.ReportProperty.IsUIDesignerColumns = false;
            this.ReportProperty.IsGroupSummary = true;
            this.ReportProperty.SimpleAllCols = false;
            this.ReportProperty.PrimaryKeyFieldName = "FNUMBER";
            this.ReportProperty.IdentityFieldName = "seq";
        }
        protected override string BuilderTempTableOrderBySQL(IRptParams filter)
        {
            string sortStr = filter.FilterParameter.SortString;
            if (sortStr.Length > 0)
            {
                return sortStr;
            }
            return " FNUMBER ";
        }
        public override void BuilderReportSqlAndTempTable(IRptParams filter, string tableName)
        {
            string seqFld = string.Format(base.KSQL_SEQ, BuilderTempTableOrderBySQL(filter));
            string createSql = @"/*dialect*/ 
            CREATE TABLE #TEMP
            (FUSEORGID         INT NULL, 
             FIDENTITYID         INT NULL,
             FORGNAME VARCHAR(255) NULL,
             FNUMBER           VARCHAR(255) NULL, 
             FMatName           VARCHAR(255) NULL, 
             FSPECIFICATION    VARCHAR(255) NULL, 
             FUnitNumber       VARCHAR(255) NULL, 
             FBASEQTY          DECIMAL(38, 10) NOT NULL, 
             FRemainStockInQty DECIMAL(38, 10) NOT NULL, 
             FPickQty          DECIMAL(38, 10) NOT NULL, 
             FPickQty3          DECIMAL(38, 10) NOT NULL, 
             FSAFESTOCK        DECIMAL(38, 10) NOT NULL, 
             FDATE             VARCHAR(50) NULL
            )
             ";
            string filterStr = "";
            if (filter.FilterParameter.FilterString.Length > 0)
            {
                filterStr = " where " + filter.FilterParameter.FilterString;
            }
            string dataSql = $" INSERT INTO #TEMP EXEC P_RK_GetPurInvReport";
            string endSql = $" select * ,{seqFld} into {tableName} from #TEMP {filterStr}";
            string sql = createSql + dataSql + endSql;
            DBUtils.ExecuteDynamicObject(this.Context, sql);
        }
        public override List<SummaryField> GetSummaryColumnInfo(IRptParams filter)
        {
            var result = base.GetSummaryColumnInfo(filter);
            result.Add(new SummaryField("FBASEQTY", Enu_SummaryType.SUM));
            result.Add(new SummaryField("FRemainStockInQty", Enu_SummaryType.SUM));
            result.Add(new SummaryField("FPickQty", Enu_SummaryType.SUM));
            result.Add(new SummaryField("FPickQty3", Enu_SummaryType.SUM));
            result.Add(new SummaryField("FSAFESTOCK", Enu_SummaryType.SUM));
            return result;
        }
    }
}
